package com.example.demo.common;

/**
 * @author niejun update 2024-09-13
 */
public class R {
    private static final long serialVersionUID = 1L;
    private static final int SUCCESS = 0;
    private static final int FAIL = 1;

    private int code;
    private String msg;
    private Object data;

    public int getCode() {
        return code;
    }
    public void setCode(int code) {
        this.code = code;
    }
    public String getMsg() {
        return msg;
    }
    public void setMsg(String msg) {
        this.msg = msg;
    }
    public Object getData() {
        return data;
    }
    public void setData(Object data) {
        this.data = data;
    }

    public R(int code, String msg, Object data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public static R ok(Object data) {
        return new R(SUCCESS, null, data);
    }
    public static R ok(Object data, String msg) {
        return new R(SUCCESS, msg, data);
    }
    public static R fail(String msg){
        return new R(FAIL, msg, null);
    }
    public static R fail(String msg, Object data){
        return new R(FAIL, msg, data);
    }

    public static R okFail(boolean b, String okMsg, String failMsg){
        if(b){
            return R.ok(null, okMsg);
        }else{
            return R.ok(failMsg);
        }
    }
    public static R okFail(boolean b){
         return okFail(b, "操作成功", "操作失败");
    }
}